define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'merchant/refund/index',
                    multi_url: 'merchant/refund/multi',
                    table: 'order_refund',
                }
            });

            var table = $("#table");
            table.on('click', '.user', function() {
                let id = $(this).data('id');
                Fast.api.open('merchant/user/info/ids/' + id, '操作', {});
            });
            table.on('click', '.order', function() {
                let id = $(this).data('id');
                Fast.api.open('merchant/order/one?id=' + id, '操作', {});
            });
            table.on('click', '.store', function() {
                let id = $(this).data('id');
                Fast.api.open('merchant/store/info/id/' + id, '操作', {});
            });
            table.on('click', '.product_list', function() {
                let id = $(this).data('id');
                Fast.api.open('merchant/refund/product?orid=' + id, '操作', {});
            });
            table.on('click', '.operate_log', function() {
                let id = $(this).data('id');
                Fast.api.open('merchant/refund/operate_log?orid=' + id, '操作', {});
            });
            table.on('click', '.agree', function() {
                let id = $(this).data('id');
                Fast.api.open('merchant/refund/agree?id=' + id, '操作', {});
            });
            table.on('click', '.refuse', function() {
                let id = $(this).data('id');
                Fast.api.open('merchant/refund/refuse?id=' + id, '操作', {});
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'OR.id',
                sortName: 'OR.id',
                sortOrder: 'desc',
                fixedColumns: true,
                fixedRightNumber: 1,

                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'uid', title: '用户ID', formatter: function(value, row, index) {
                            return '<a class="user" href="javascript:" data-id="' + row.uid + '">' + row.uid + '</a>';
                        }}, 
                        {field: 'oid', title: '订单ID', formatter: function(value, row, index) {
                            return '<a class="order" href="javascript:" data-id="' + row.oid + '">' + row.oid + '</a>';
                        }},
                        {field: 'OR.refund_out_trade_no', title: '退款单ID', operate: 'like', formatter: function(value, row, index){
                            return row.refund_out_trade_no;
                        }},
                        {field: 'sid', title: '店铺ID', formatter: function(value, row, index) {
                            return '<a class="store" href="javascript:" data-id="' + row.sid + '">' + row.sid + '</a>';
                        }},
                        {field: '', title: '退款商品列表', searchable: false, formatter: function(value, row, index) {
                            return '<a class="product_list" href="javascript:" data-id="' + row.id + '">点击查看退款商品列表</a>';
                        }},
                        {field: 'status', title: '退款状态', formatter: function(value, row, index) {
                            return row.status_text;
                        }, searchList: window.status_options},
                        {field: '', title: '操作记录', searchable: false, formatter: function(value, row, index) {
                            return '<a class="operate_log" href="javascript:" data-id="' + row.id + '">操作记录列表</a>';
                        }},
                        {field: 'createtime', title: '创建时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'updatetime', title: '修改时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: '', title: '操作', formatter: function(value, data, index) {
                            if(data.status == 7 || data.status == 13) {
                                return '<button id="agree" class="btn btn-info agree" data-id="' + data.id + '">同意退款</button>&nbsp;&nbsp;<button id="refuse" class="btn btn-info refuse" data-id="' + data.id + '">拒绝退款</button>';
                            }
                        }}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        product: function() {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'merchant/refund/product?orid=' + window.orid,
                    multi_url: 'merchant/refund/multi',
                    table: 'order_refund_product',
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'ORP.id',
                sortName: 'ORP.id',
                sortOrder: 'desc',
                fixedColumns: true,
                fixedRightNumber: 0,

                columns: [
                    [
                        {checkbox: true},
                        {field: 'ORP.id', title: __('Id'), sortable: true, formatter: function(value, row, index) {
                            return row.id;
                        }},
                        {field: 'SP.name', title: '产品名称',formatter: function(value, row, index) {
                            return row.name;
                        }, operate: 'LIKE'},
                        {field: 'SP.store_name', title: '店铺名称',formatter: function(value, row, index) {
                            return row.store_name;
                        }, operate: 'LIKE'},
                        {field: 'SP.classify', title: '产品分类',formatter: function(value, row, index) {
                            return row.classify;
                        }, operate: 'LIKE'},
                        {field: 'SPS.name', title: '产品规格',formatter: function(value, row, index) {
                            return row.psname;
                        }, operate: 'LIKE'},
                        {field: 'photo', title: '产品主图', events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'SP.selling', title: '产品卖点', formatter: function(value, row, index) {
                            return row.selling;
                        }, operate: 'LIKE'},
                        {field: 'SPS.price', title: '产品价格',formatter: function(value, row, index) {
                            return row.price;
                        }},
                        {field: 'SP.ost_sale', title: '产品售后', formatter: function(value, row, index) {
                            return row.ost_sale;
                        }, operate: 'LIKE'},
                        {field: 'ORP.createtime', title: '创建时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'ORP.updatetime', title: '修改时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        operate_log: function() {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'merchant/refund/operate_log?orid=' + window.orid,
                    multi_url: 'merchant/refund/multi',
                    table: 'order_refund_product',
                }
            });

            var table = $("#table");

            table.on('click', '.relatedtype-0', function() {
                let id = $(this).data('id');
                Fast.api.open('merchant/user/info/ids/' + id, '操作', {});
            });

            table.on('click', '.relatedtype-1', function() {
                let id = $(this).data('id');
                Fast.api.open('merchant/store/info/id/' + id, '操作', {});
            });

            table.on('click', '.relatedtype-2', function() {
                let id = $(this).data('id');
                Fast.api.open('auth/admin/info?id=' + id, '操作', {});
            });

            table.on('click', '.relatedtype-3', function() {
                let id = $(this).data('id');
                Fast.api.open('merchant/refund/operate_log?orid=' + id, '操作', {});
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'ORL.id',
                sortName: 'ORL.id',
                sortOrder: 'desc',
                fixedColumns: true,
                fixedRightNumber: 0,

                columns: [
                    [
                        {checkbox: true},
                        {field: 'ORL.id', title: __('Id'), sortable: true, formatter: function(value, row, index) {
                            return row.id;
                        }},
                        {field: 'ORL.type', title: '操作类型', formatter: function(value, row, index) {
                            return row.type_text;
                        }, searchList: window.operate_type_options},
                        {field: 'relatedid', title: '关联ID', formatter: function(value, row, index) {
                            return '<a href="javascript:" class="business_license relatedtype-' + row.type + '" data-id="' + row.relatedid + '">' + row.relatedid + '</a>';
                        }},
                        {field: 'name', title: '操作', operate: 'LIKE'},
                        {field: 'remark', title: '备注', operate: 'LIKE'},
                        {field: 'images_url', title: '相关图片', events: Table.api.events.image, formatter: function(value, data, index){
                            let row = Table.api.formatter.image(value, data, index);
                            return row;
                        }, operate: false},
                        {field: 'createtime', title: '创建时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'updatetime', title: '修改时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        agree: function() {
            Controller.api.bindevent();
        },
        refuse: function() {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});